<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="110px" label-position="left">
                    <el-form-item label="会员名" prop="name">
                         <el-input type="text" v-model="form.name" placeholder="会员名" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="手机号" prop="mobile">
                         <el-input type="text" v-model="form.mobile" placeholder="手机号" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="性别" prop="sex">
                         <el-input type="text" v-model="form.sex" placeholder="性别" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="会员等级" prop="level">
                         <el-input type="text" v-model="form.level" placeholder="会员等级" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="微信openid" prop="openid">
                         <el-input type="text" v-model="form.openid" placeholder="微信openid" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="微信unionid" prop="unionid">
                         <el-input type="text" v-model="form.unionid" placeholder="微信unionid" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="所属应用" prop="appid">
                         <sc-select v-model="form.appid" :apiObj="$API.wechat.account.getSelectItem" placeholder="请选择所属应用" clearable filterable ></sc-select>
                    </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaving: false,
				//表单数据
                form: {
                  name: "",
                  mobile: "",
                  sex: "",
                  level: "",
                  openid: "",
                  unionid: "",
                  appid: "",
                  },
              //验证规则
              rules: {
                name: [
                  {required: true, message: '请输入会员名'}
                ],
                mobile: [
                  {required: true, message: '请输入手机号'}
                ],
                sex: [
                  {required: true, message: '请输入性别'}
                ],
                level: [
                  {required: true, message: '请输入会员等级'}
                ],
                openid: [
                  {required: true, message: '请输入微信openid'}
                ],
                unionid: [
                  {required: true, message: '请输入微信unionid'}
                ],
                appid: [
                  {required: true, message: '请输入所属应用'}
                ],
                },
                }
		},
		mounted() {
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaving = true;
						let res;
						if(this.mode === 'edit')res = await this.$API.member.member.edit.post(this.form)
						else if(this.mode === 'add')res = await this.$API.member.member.save.post(this.form)
						this.isSaving = false;
						if(res.success){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}
					}
				})
			},
			//表单注入数据
			setData(data){
			    Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>